package com.kins.executer.handlers.common.base;

import com.alibaba.fastjson.JSONObject;
import com.kins.executer.constants.Constant;
import com.kins.executer.entitys.SOFlowRectsEntity;
import com.kins.executer.utils.DataUtils;
import com.kins.executer.vo.MappingParamVo;
import io.micrometer.common.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description:
 * @Author: 申科
 * @Date:2025/11/12 15:43
 */

public class AiModelExecuterHandler {
    /**
     * 创建aiModel入参参数
     */

    public Map<String, Object> buildParam(Map<String, Object> inputMap, MappingParamVo vo) throws Exception {
        //模型提供方
        String modelProvider = MapUtils.getString(inputMap, "model_provider");
        //模型名称
        String modelName = MapUtils.getString(inputMap, "model_name");

        if(StringUtils.isEmpty(modelProvider) || StringUtils.split(modelName, Constant.COMMA).length < 1){
            throw new Exception("模型信息未选择，无法进行后续操作");
        }
        //根据
        ServiceInfoVo infoVo = (ServiceInfoVo) vo.getDataMap().get(Constant.SERVICE_ARRANGE_INFO);
        vo.setServiceArrangeId(infoVo.getServiceArrangeId());
        vo.setServiceArrangeVersion(infoVo.getServiceArrangeVersion());
        //测试执行
        Map<String, SOFlowRectsEntity> rectsMap = (Map<String, SOFlowRectsEntity>) vo.getDataMap().get(Constant.RECTS_MAP);
        if(MapUtils.isEmpty(rectsMap)){
            //日终执行
            rectsMap = AppConfiguration.soNodeMap.get(
                    DataUtils.getJoinStr(Constant.UNDERLINE,infoVo.getPath(),infoVo.getMethod()));//根据拼接标志凭借字符串

        }
        //ai模型配置信息
        List<AiModelConfigEntity> aiModelConfigs = (List<AiModelConfigEntity> vo.getDataMap().get(Constant.AI_MODEL_CONFIG));
        if(CollectionUtils.isEmpty(aiModelConfigs)){
            aiModelConfigs = AppConfiguration.modelconfiList;
        }
        String modelUrl = "";
        if(CollectionUtils.isNotEmpty(aiModelConfigs)){
            for(AiModelConfigEntity aiModelConfig : aiModelConfigs){
                if(modelProvider.equals(aiModelConfig.getModelProvider()) && modelName.equals(aiModelConfig.getModelName())){
                    modelUrl = aiModelConfig.getModelUrl();//模型访问路径
                    break;
                }
            }
        }
        inputMap.put("model_url", modelUrl);
        List<Map<String, Object>> prompts = new ArrayList<>();
        if(MapUtils.isNotEmpty(rectsMap)){
            SOFlowRectsEntity rect = getFlowRects(vo, rectsMap);//服务编排流程节点信息
            //系统信息
            String llmSystemInfo = rect.getLlmSystemInfo();//LLm-系统信息
            //问答信息
            String llmAnwerInfo = rect.getLlmAnwerInfo();//llm-问答信息
            if(StringUtils.isNotEmpty(llmSystemInfo)){
                Map<String, Object> prompt = new HashMap<>();
                ActionMessageInfo actionMessageInfo = JSONObject.parseObject(llmSystemInfo, ActionMessageInfo.class);
                prompt.put("role",actionMessageInfo.getRole());
                String text = DataUtils.replaceParam(actionMessageInfo.getContent(),inputMap);//替换参数
                prompt.put("text",text);
                prompts.add(prompt);
            }
            if(StringUtils.isNotEmpty(llmAnwerInfo)){
                List<ActionMessageInfo> answerList = JSONObject.parseArray(llmAnwerInfo, ActionMessageInfo.class);
                if(CollectionUtils.isNotEmpty(answerList)){
                    for(ActionMessageInfo actionMessageInfo : answerList){
                        Map<String, Object> prompt = new HashMap<>();
                        prompt.put("role",actionMessageInfo.getRole());
                        String text = DataUtils.replaceParam(actionMessageInfo.getContent(),inputMap);//替换参数
                        prompt.put("text",text);
                        prompts.add(prompt);
                    }
                }
            }
            inputMap.put("prompts", prompts);
        }
        if (CollectionUtils.isEmpty(prompts)){
            throw new Exception("模型信息未选择，无法进行后续操作");
        }
        return inputMap;
    }
    private SOFlowRectsEntity getFlowRects(MappingParamVo vo, Map<String, SOFlowRectsEntity> rectsMap){
        SOFlowRectsEntity soFlowRectsEntity = rectsMap.get(DataUtils.getRectsKeyStr(vo));
        if(soFlowRectsEntity != null){
            return soFlowRectsEntity;
        }
        String nodeId = vo.getNodeId();
        if(StringUtils.isEmpty(nodeId) || MapUtils.isEmpty(rectsMap)){
            return null;
        }
        for (String s : rectsMap.keySet()){
            if(s.contains(nodeId)){
                return rectsMap.get(s);
            }
        }
        return null;
    }
}
